package com.acme.domain.account;

import java.io.Serializable;

import com.acme.exceptions.NoEnoughFundsException;

public abstract class AbstractAccount implements Account,
	Comparable<AbstractAccount>, Serializable {

    private static final long serialVersionUID = 1L;

    public static int getUniqueAccountNumber() {
	return (int) System.currentTimeMillis();
    }

    protected int id;
    protected double balance;

    protected AccountState state;

    public AbstractAccount(final int id, final double amount) {
	balance = amount;
	this.id = id;
	state = AccountState.OPENED;
    }

    @Override
    public int compareTo(final AbstractAccount account) {

	if (account.getAccountNumber() < id) {
	    return -1;
	} else if (account.getAccountNumber() > id) {
	    return 1;
	}

	return 0;
    }

    @Override
    public long decimalValue() {
	return Math.round(getBalance());
    }

    @Override
    public abstract void deposit(final double amount)
	    throws IllegalArgumentException;

    @Override
    public int getAccountNumber() {
	return id;
    }

    @Override
    public double getBalance() {
	return balance;
    }

    @Override
    public double getMaximumAmountToWithdraw() {
	return getBalance();
    }

    public AccountState getState() {
	return state;
    }

    @Override
    public boolean isOpened() {
	return state == AccountState.OPENED;
    }

    public void setState(final AccountState state) {
	this.state = state;
    }

    @Override
    public abstract void withdraw(final double amount)
	    throws NoEnoughFundsException;
}
